package com.toune.movie.mvp.activity;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.ta.utdid2.android.utils.NetworkUtils;
import com.toune.movie.R;
import com.toune.movie.base.BaseActivity;
import com.toune.movie.base.BasePresenterImpl;
import com.toune.movie.base.myUtil.MyUtil;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity2 extends BaseActivity {
    @BindView(R.id.et_phone)
    AutoCompleteTextView etPhone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.email_sign_in_button)
    Button emailSignInButton;
    @BindView(R.id.login_form)
    ScrollView loginForm;
    @BindView(R.id.forget_psw_tv)
    TextView forgetPswTv;
    @BindView(R.id.register_tv)
    TextView registerTv;
    private String unicomPhoneNumber;

    private void checklogin(final String mobile) {
        MyUtil.showProDialog(context);
        AVUser.loginByMobilePhoneNumberInBackground(mobile, password.getText().toString(), new LogInCallback<AVUser>() {
            @Override
            public void done(AVUser avUser, AVException e) {
                if (e == null) {
                    RxToast.success("欢迎回来");
                    MyUtil.hidProDialog();
                    finish();
                } else {
                    RxToast.error(e.getMessage());
                }
            }
        });
    }

    @Override
    public BasePresenterImpl initPresenter() {
        return null;
    }

    @Override
    public void loadMore() {

    }

    @Override
    public void refreData() {

    }

    @Override
    public void init(Bundle savedInstanceState) {
        emailSignInButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                unicomPhoneNumber = etPhone.getText().toString();

                if ((unicomPhoneNumber == null || unicomPhoneNumber.isEmpty())) {
                    RxToast.info("您输入的手机号不能为空");
                    return;
                }
                if ((unicomPhoneNumber == null) || (unicomPhoneNumber.isEmpty()) || (unicomPhoneNumber.length() != 11)) {
                    RxToast.info("请输入正确的手机号");
                    return;
                }
                if (!NetworkUtils.isConnected(context)) {
                    RxToast.info("网络连接异常，请稍后重试");
                    return;
                }
                checklogin(unicomPhoneNumber);
            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login2;
    }

    @Override
    protected String getTitleStr() {
        return "登录";
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void useNightMode(boolean isNight) {

    }

    @OnClick({R.id.forget_psw_tv, R.id.register_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forget_psw_tv:
                break;
            case R.id.register_tv:
                RxActivityTool.skipActivity(context, RegisteActivity.class);
                break;
        }
    }
}

